<template>
  <div>
    <h3>商品审核列表（数据来源ES）</h3>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入关键字 "
            clearable
          />
        </el-form-item>
        <el-form-item label="品牌">
          <el-select
            v-model="searchForm.brandId"
            placeholder="请选择品牌"
            style="width: 120px"
            clearable
          >
            <el-option
              v-for="item in prodListOptions"
              :label="item.brandName"
              :value="item.brandId"
              :key="item.brandId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="日期">
          <el-date-picker
            v-model="searchForm.beginTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开始时间"
          />--
          <el-date-picker
            v-model="searchForm.endTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择结束时间"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="localProdEs()">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="prodId" label="ID" width="180" />
      <el-table-column prop="prodName" label="名称" width="180">
        <template #default="scope">
          <span v-html="scope.row.prodName"></span>
        </template>
      </el-table-column>
      <el-table-column prop="typeName" label="类型" width="180" />
      <el-table-column prop="brandName" label="品牌" />
      <el-table-column prop="unitName" label="单位" />
      <el-table-column prop="kindName" label="种类" />
      <el-table-column prop="tagName" label="标签" />
      <el-table-column prop="apprName" label="供应商">
        <template #default="scope">
          <span v-html="scope.row.apprName"></span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" />
    </el-table>

    <div class="demo-pagination-block" style="margin-top: 20px">
      <el-pagination
        v-model:current-page="searchForm.pageNum"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { getProdListEs, getBrandList } from "@/api/api";
import { onMounted, ref } from "vue";

const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  prodId: null,
  brandId: null,
  beginTime: null,
  endTime: null,
});
const total = ref(0);
const tableData = ref([]);
const prodListOptions = ref([]);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  localProdEs();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  localProdEs();
};

const localProdEs = () => {
  getProdListEs(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  localProdEs();

  getBrandList({ pageNum: 1, pageSize: 10000 }).then((res) => {
    if (res.data.code == 200) {
      prodListOptions.value = res.data.data.records;
    }
  });
});
</script>

<style scoped></style>
